<?php

namespace app\common\repository\book;

use think\Db;
use app\common\model\book\Book as BookModel;

class R
{
    public function __construct(
        BookModel $bookModel
    )
    {
        $this->bookModel = $bookModel;
    }

    public function getBook($cond)
    {
        return BookModel::where($cond)
            ->with(['section' => ['article']])
            ->find();
    }

    public function getBooks($cond)
    {
        return BookModel::where($cond)
            ->with(['section'])
            ->select();
    }

    public function getHotBooks()
    {
        return BookModel::where([])
            ->with(['user' => ['profile']])
            ->limit(5)
            ->select();
    }

    public function getUserHotBooks($userId)
    {
        return BookModel::where(['user_id' => $userId])
            ->with(['user' => ['profile']])
            ->limit(5)
            ->select();
    }

    public function getBooksWithPage($cond, $order, $page)
    {
        $count = count(BookModel::where($cond)
            ->select());
        $list = BookModel::where($cond)
            ->with(['user' => ['profile']])
            ->order($order)
            ->limit($page['size'])
            ->page($page['index'])
            ->select();
        return ['count' => $count, 'list' => $list];
    }
}
